﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HandRise .MBHOP.Controls;
using Handrise.CDCable.Mobile.Business;
using System.IO;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmRecording : FrmBase
    {
        private string parentForm;
        private string linkID;
        private LinkFile linkFile;
        private LinkFileService linkFileService = new LinkFileService();

        private bool needSave = false ;

        public FrmRecording()
        {
            InitializeComponent();
            ttlbrhead.Text = ResourceImage.地面巡视;
            ilstbcup.Images.Add(ResourceImage.保存01);
            ilstbcup.Images.Add(ResourceImage.查看);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.保存02);
            ilstdown.Images.Add(ResourceImage.查看1);
            ilstdown.Images.Add(ResourceImage.返回02);
        }

        private void FrmRecording_Load(object sender, EventArgs e)
        {
            linkFileService.OnPlaying += new PlayingHandler(linkFileService_OnPlaying);
            linkFileService.OnRecording += new RecordingHandler(linkFileService_OnRecording);

            if (IsReturn)
                LoadCacheData();
            else
                LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            parentForm = (string)Parameter[0];
            linkID = (string)Parameter[1];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            parentForm = (string)Caches[0];
            linkID = (string)Caches[1];
        }

        private void bcxunjian_OnButtonClick(object sender, int buttonIndex)
        {
            //如果正在播放或正在录音，先停止
            string state = linkFileService.GetAudioState();
            if (state != "Init")
                btnStop_Click(sender, null);

            switch (buttonIndex)
            {
                case 0:  //保存
                    if(SaveRecord())
                        mssdlgexec.ShowDialog(this, "录音已保存！", DialogType.Confirm, FormManage.form_Deactivate);
                    break;
                case 1: //查看
                    ViewRecord();
                    break;
                case 2:
                    ReturnPriorForm();
                    break;
            }
        }

        /// <summary>
        /// 保存录音
        /// </summary>
        private bool SaveRecord()
        {
            if (linkFile == null)
                return false ;

            linkFileService.AddLinkFile(linkFile);
            needSave = false;

            //根据父窗体来确认是否要更改关联文件对应信息的上报状态
            switch (parentForm)
            {
                case "FrmPatrolException":  //巡视异常信息的录音
                case "FrmElseWork":         //其他工作的录音
                    PatrolInfoService patrolInfoService = new PatrolInfoService();
                    PatrolDetail patrolDetail = patrolInfoService.GetPatrolDetail(linkFile.LinkID);
                    patrolDetail.ReportState = "未上报";
                    patrolInfoService.UpdatePatrolDetail(patrolDetail);
                    break;
                default:
                    break;
            }

            return true;
        }

        /// <summary>
        /// 查看录音
        /// </summary>
        private void ViewRecord()
        {
            if (needSave)
            {
                if (mssdlgSaveRecord.ShowDialog(this, "是否保存当前录音？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
                    SaveRecord();
            }
            FormManage.GotoNext("FrmRecordLst", new object[] { "FrmRecording", "'" + linkID + "'" }, new object[] { parentForm ,linkID}, false);
        }

        /// <summary>
        /// 返回父窗体
        /// </summary>
        private void ReturnPriorForm()
        {
            if (needSave)
            {
                if (mssdlgSaveRecord.ShowDialog(this, "是否保存当前录音？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
                    SaveRecord();
            }
            FormManage.GotoNext(parentForm, null,null,true);

        }

        private void linkFileService_OnPlaying(int playSec,bool isStop)
        {
            lblTime.Text = FormatSec(playSec);
        }

        private void linkFileService_OnRecording(int recordSec, bool isStop)
        {
            lblTime.Text = FormatSec(recordSec);
        }

        /// <summary>
        /// 格式化秒未00:00格式
        /// </summary>
        /// <param name="sec"></param>
        /// <returns></returns>
        private string FormatSec(int sec)
        {
            int min = (sec / 60);
            int restSec = (sec % 60);
            return String.Format("{0:D2}", min) + ":" + String.Format("{0:D2}", restSec);
        }

        private void SetButtonState()
        {
        }

        private void btnRecord_Click(object sender, EventArgs e)
        {
            string state = linkFileService.GetAudioState();
            if (state != "Init")
                return;

            if (needSave)
            {
                if (mssdlgSaveRecord.ShowDialog(this, "是否放弃当前录音，重新录制？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button2)
                    return;
            }

             linkFile = linkFileService.Record(linkID);

             needSave = true;
             lblInfo.Text = "录音时间";
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            string state = linkFileService.GetAudioState();
            switch (state)
            {
                case "Recording": //正在录音
                    linkFileService.PauseRecord();
                    break;
                case "Playing":  //正在播放
                    linkFileService.PausePlay();
                    break;
                case "RecordPause":  //录音暂停
                    linkFileService.RestartRecord();
                    break;
                case "PlayPause":  //播放暂停
                    linkFileService.RestartPlay();
                    break;                
            }
           
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            string state = linkFileService.GetAudioState();
            if (state != "Init")
                return;

            if (linkFile == null)
                return;

            lblInfo.Text = "播放时间";

            if(File.Exists(linkFile.FullFileName))
                linkFileService.Play(linkFile.FullFileName);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            string state = linkFileService.GetAudioState();

            if (state == "Recording" || state == "RecordPause")
            { 
                //正在录音或暂停录音
                linkFileService.StopRecord();
            }
            else if (state == "Playing" || state == "PlayPause")
            {
                //正在播放或暂停播放
                linkFileService.StopPlay();
            }
        }





    }
}